package com.gt.quality.system.controller;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.gt.quality.base.BaseCommonController;
import com.gt.quality.base.BaseCommonService;
import com.gt.quality.config.utils.LoginHelper;
import com.gt.quality.config.utils.LoginUser;
import com.gt.quality.system.entity.SysDept;
import com.gt.quality.base.pojo.LayerTreeSelect;
import com.gt.quality.base.pojo.LayerListViewObject;
import com.gt.quality.base.pojo.Resp;
import com.gt.quality.system.service.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 部门表(SysDept)表控制层
 *
 * @author makejava
 * @since 2026-11-25 16:30:08
 */
@RestController
@RequestMapping("sysDept")
@Tag(name = "部门管理")
public class SysDeptController extends BaseCommonController<SysDept> {
    /**
     * 服务对象
     */
    @Resource
    private SysDeptService sysDeptService;

    @Override
    protected void extendBeforeBySave(SysDept obj) {
        Long parentId = obj.getParentId();
        if (parentId != null && parentId > 0) {
            SysDept parent = sysDeptService.getById(parentId);
            String ancestors = parent.getAncestors();
            ancestors = ancestors + "," + parent.getDeptId();
            obj.setAncestors(ancestors);
        }
        // 部门编码
        obj.setDeptCategory(StringUtils.isEmpty(obj.getDeptCategory())? "Lay_Bm_" + LocalDateTimeUtil.format(LocalDate.now(), "yyyyMMddHHmmss") : obj.getDeptCategory());
        LoginUser loginUser = LoginHelper.getLoginUser();
        assert loginUser != null;
        obj.setCreateDept(loginUser.getDeptId());
        obj.setCreateBy(loginUser.getUserId());
        obj.setUpdateBy(loginUser.getUserId());
        obj.setUpdateTime(LocalDateTime.now());
        obj.setCreateTime(LocalDateTime.now());
        super.extendAfterBySave(obj);
    }


    /**
     * 分页查询数据
     *
     * @return 所有数据
     */
    @PostMapping("/queryTree")
    @Operation(summary = "部门树查询")
    public Resp<LayerListViewObject> queryTree(@RequestParam(value = "userId", required = false) Long userId) {
        List<SysDept> list = sysDeptService.list();
        LayerListViewObject data = sysDeptService.buildTreeDeptInfo(list, userId);
        return Resp.ok(data);
    }

    /**
     * 部门树查询
     *
     * @return 所有数据
     */
    @PostMapping("/queryToTree")
    @Operation(summary = "部门树查询")
    public Resp<List<LayerTreeSelect>> queryToTree(@RequestParam(value = "deptId", required = false) Long deptId) {
        List<SysDept> list = sysDeptService.list();
        List<LayerTreeSelect> data = sysDeptService.queryToTree(list, deptId);
        return Resp.ok(data);
    }

    /**
     * 分页查询数据
     *
     * @return 所有数据
     */
    @PostMapping("/selectDeptTree")
    @Operation(summary = "部门树")
    public Resp<LayerListViewObject> selectDeptTree() {
        LayerListViewObject data = sysDeptService.buildTree();
        return Resp.ok(data);
    }

    @Override
    public BaseCommonService<SysDept> getThisService() {
        return sysDeptService;
    }


    @Override
    protected boolean isSuperExcel() {
        return false;
    }
}
